package org.jeecg.modules.demo.yxwarehouse.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.customer.entity.YxCustomer;
import org.jeecg.modules.demo.yxwarehouse.entity.YxWarehouse;
import org.jeecg.modules.demo.yxwarehouse.service.IYxWarehouseService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 运销仓库
 * @Author: jeecg-boot
 * @Date:   2023-06-07
 * @Version: V1.0
 */
@Api(tags="运销仓库")
@RestController
@RequestMapping("/yxwarehouse/yxWarehouse")
@Slf4j
public class YxWarehouseController extends JeecgController<YxWarehouse, IYxWarehouseService> {
	@Autowired
	private IYxWarehouseService yxWarehouseService;
	
	/**
	 * 分页列表查询
	 *
	 * @param yxWarehouse
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "运销仓库-分页列表查询")
	@ApiOperation(value="运销仓库-分页列表查询", notes="运销仓库-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(YxWarehouse yxWarehouse,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		try{
			QueryWrapper<YxWarehouse> queryWrapper = QueryGenerator.initQueryWrapper(yxWarehouse, req.getParameterMap());
			Page<YxWarehouse> page = new Page<YxWarehouse>(pageNo, pageSize);
			IPage<YxWarehouse> pageList = yxWarehouseService.page(page, queryWrapper);
			return Result.OK(pageList);
		}catch (Exception e){
			e.printStackTrace();
			return Result.error(300,"网络故障！");
		}
	}
	
	/**
	 *   添加
	 *
	 * @param yxWarehouse
	 * @return
	 */
	@AutoLog(value = "运销仓库-添加")
	@ApiOperation(value="运销仓库-添加", notes="运销仓库-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody YxWarehouse yxWarehouse) {
		try{
			yxWarehouse.setDelFlag(0);
			yxWarehouseService.save(yxWarehouse);
			return Result.OK("添加成功！");
		}catch (Exception e){
			e.printStackTrace();
			return  Result.error(500,"系统错误！");
		}
	}
	
	/**
	 *  编辑
	 *
	 * @param yxWarehouse
	 * @return
	 */
	@AutoLog(value = "运销仓库-编辑")
	@ApiOperation(value="运销仓库-编辑", notes="运销仓库-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody YxWarehouse yxWarehouse) {
		try{
			yxWarehouseService.updateById(yxWarehouse);
			return Result.OK("编辑成功!");
		}catch (Exception e){
			e.printStackTrace();
			return  Result.error(500,"系统错误！");
		}
	}

	 @GetMapping(value = "/getWarehouseList")
	 public  Result<?> getWarehouseList(){
		 List<YxWarehouse> warehouses = yxWarehouseService.getWarehouseList();
		 return Result.ok(warehouses);
	 }

	 @RequestMapping(value = "/revertLogicDeletedWarehouse", method = RequestMethod.PUT)
	 public Result revertLogicDeletedWarehouse(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
		 String warehouseIds = jsonObject.getString("warehouseIds");
		 if (StringUtils.isNotBlank(warehouseIds)) {
			 yxWarehouseService.revertLogicDeletedWarehouse(Arrays.asList(warehouseIds.split(",")));
		 }
		 return Result.ok("还原成功");

	 }

	 /**
	  *   冻结
	  *
	  * @param yxWarehouse
	  * @return
	  */
	 @AutoLog(value = "运销仓库-禁用")
	 @ApiOperation(value="运销仓库-禁用", notes="运销仓库-禁用")
	 @PostMapping(value = "/freeze")
	 public Result<?> freeze(@RequestBody  YxWarehouse yxWarehouse) {
		 try {
			 if(yxWarehouse.getStatus()==2){
				 return Result.error(300,"已是禁用状态！");
			 }
			 yxWarehouse.setStatus(2);
			 yxWarehouseService.updateById(yxWarehouse);
			 return Result.OK("已禁用！");
		 }catch (Exception e){
			 e.printStackTrace();
			 return Result.error(500,"系统错误！");
		 }

	 }
	 /**
	  *   解冻
	  *
	  * @param yxWarehouse
	  * @return
	  */
	 @AutoLog(value = "运销仓库-启用")
	 @ApiOperation(value="运销仓库-启用", notes="运销仓库-启用")
	 @PostMapping(value = "/unfreeze")
	 public Result<?> unfreeze(@RequestBody YxWarehouse yxWarehouse) {
		 try {
			 if(yxWarehouse.getStatus()==1){
				 return Result.error(300,"已是启用状态！");
			 }
			 yxWarehouse.setStatus(1);
			 yxWarehouseService.updateById(yxWarehouse);
			 return Result.OK("已启用！");
		 }catch (Exception e){
			 e.printStackTrace();
			 return Result.error(500,"系统错误！");
		 }

	 }
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "运销仓库-通过id删除")
	@ApiOperation(value="运销仓库-通过id删除", notes="运销仓库-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		try{
			yxWarehouseService.removeById(id);
			return Result.OK("删除成功!");
		}catch (Exception e){
			e.printStackTrace();
			return  Result.error(500,"系统错误！");
		}
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "运销仓库-批量删除")
	@ApiOperation(value="运销仓库-批量删除", notes="运销仓库-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.yxWarehouseService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "运销仓库-通过id查询")
	@ApiOperation(value="运销仓库-通过id查询", notes="运销仓库-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		YxWarehouse yxWarehouse = yxWarehouseService.getById(id);
		if(yxWarehouse==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(yxWarehouse);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param yxWarehouse
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, YxWarehouse yxWarehouse) {
        return super.exportXls(request, yxWarehouse, YxWarehouse.class, "运销仓库");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, YxWarehouse.class);
    }

}
